package model

import (
	"time"

	"gorm.io/gorm"
)

// UserProjectRelation 用户与项目关系表
type UserProjectRelation struct {
	ID        uint64    `gorm:"primaryKey"`
	UserID    uint64    `gorm:"not null;index:idx_user_project"`
	ProjectID uint64    `gorm:"not null;index:idx_user_project"`
	Role      int8      `gorm:"type:tinyint;not null;comment:'关系类型 1:发起人 2:捐款人 3:关注人'"`
	CreatedAt time.Time `gorm:"autoCreateTime"`
	UpdatedAt time.Time `gorm:"autoUpdateTime"`

	// 关联
	User    User    `gorm:"foreignKey:UserID"`
	Project Project `gorm:"foreignKey:ProjectID"`
}

// Favorite 收藏表
type Favorite struct {
	ID        uint64         `gorm:"primaryKey"`
	UserID    uint64         `gorm:"not null;index:idx_user_project"`
	ProjectID uint64         `gorm:"not null;index:idx_user_project"`
	CreatedAt time.Time      `gorm:"autoCreateTime"`
	DeletedAt gorm.DeletedAt `gorm:"index"`

	// 关联
	User    User    `gorm:"foreignKey:UserID"`
	Project Project `gorm:"foreignKey:ProjectID"`
}

// Role 角色表
type Role struct {
	ID          uint      `gorm:"primaryKey"`
	Name        string    `gorm:"type:varchar(50);not null;uniqueIndex"`
	Description string    `gorm:"type:varchar(200)"`
	CreatedAt   time.Time `gorm:"autoCreateTime"`
	UpdatedAt   time.Time `gorm:"autoUpdateTime"`
}

// Permission 权限表
type Permission struct {
	ID          uint      `gorm:"primaryKey"`
	Name        string    `gorm:"type:varchar(50);not null;uniqueIndex"`
	Description string    `gorm:"type:varchar(200)"`
	Path        string    `gorm:"type:varchar(100)"`
	Method      string    `gorm:"type:varchar(10)"` // HTTP方法：GET, POST等
	CreatedAt   time.Time `gorm:"autoCreateTime"`
}

// UserRole 用户角色关联表
type UserRole struct {
	ID        uint64    `gorm:"primaryKey"`
	UserID    uint64    `gorm:"not null;index:idx_user_role"`
	RoleID    uint      `gorm:"not null;index:idx_user_role"`
	CreatedAt time.Time `gorm:"autoCreateTime"`

	// 关联
	User User `gorm:"foreignKey:UserID"`
	Role Role `gorm:"foreignKey:RoleID"`
}

// RolePermission 角色权限关联表
type RolePermission struct {
	ID           uint64    `gorm:"primaryKey"`
	RoleID       uint      `gorm:"not null;index:idx_role_perm"`
	PermissionID uint      `gorm:"not null;index:idx_role_perm"`
	CreatedAt    time.Time `gorm:"autoCreateTime"`

	// 关联
	Role       Role       `gorm:"foreignKey:RoleID"`
	Permission Permission `gorm:"foreignKey:PermissionID"`
}
